package com.ulife.permission.manager.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.common.UserQueryResult;
import com.ulife.permission.dao.UserDao;
import com.ulife.permission.dao.dataobject.UserDO;
import com.ulife.permission.manager.UserManager;

public class UserManagerImpl implements UserManager {
	private static final Logger log = LoggerFactory.getLogger(UserManagerImpl.class);

	@Autowired
	private UserDao userDao;
	
	public Result<UserDO> login(String userName, String password) throws ManagerException {
		Result<UserDO> result = new Result<UserDO>();
		try {
			UserDO user = userDao.getUserByNameAndPassword(userName, password);
			if (user != null && user.getStatus() == 0) {
				result.setSuccess(true);
				result.setModule(user);
			}
		} catch (SQLException e) {
			log.error("queryUserByNameAndPassword exception", e);
			throw new ManagerException(e);
		}
		
		return result;
	}

	public Result<UserDO> getUserById(long userId) throws ManagerException {
		Result<UserDO> result = new Result<UserDO>();
		try {
			UserDO user = userDao.getUserById(userId);
			if (user != null) {
				result.setSuccess(true);
				result.setModule(user);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<UserDO> addUser(UserDO user) throws ManagerException {
		Result<UserDO> result = new Result<UserDO>();
		try {
			int id = userDao.addUser(user);
			if (id > 0) {
				result.setSuccess(true);
				result.setModule(user);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public UserQueryResult getAllUser() throws ManagerException {
		UserQueryResult result = new UserQueryResult();
		List<UserDO> userList;
		try {
			userList = userDao.getAllUser();
			if (CollectionUtils.isNotEmpty(userList)) {
				result.setUserList(userList);
			}
			result.setSuccess(true);
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<Integer> delUser(long userId) throws ManagerException {
		Result<Integer> result = new Result<Integer>();
		try {
			int i = userDao.delUser(userId);
			if (i > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<Integer> updateUser(UserDO user) throws ManagerException {
		Result<Integer> result = new Result<Integer>();
		try {
			int i = userDao.updateUser(user);
			if (i > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public UserQueryResult getAllAvailableUsers() throws ManagerException {
		UserQueryResult result = new UserQueryResult();
		List<UserDO> userList;
		try {
			userList = userDao.getAllAvailableUsers();
			if (CollectionUtils.isNotEmpty(userList)) {
				result.setUserList(userList);
			}
			result.setSuccess(true);
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

}
